#include "driver.h"
#include "..\states.h"

PSP_MODULE_INFO("USBDRIVER", PSP_MODULE_KERNEL, 1, 0);

int register_udriver() { k1s; resetdata(pdata); pdata->p = &PS3HubDriver; int ret = sceUsbbdRegister(&PS3HubDriver); k1r; return ret; }
int start_mainusbbus() { k1s; int ret = sceUsbStart("USBBusDriver", 0, 0 ); k1r; return ret; }
int start_mainusbdrv() { k1s; int ret = sceUsbStart(PS3HubDriver.name, 0, 0 ); k1r; return ret; }
int activate_usbdriv() { k1s; int ret = sceUsbActivate(0xCCCC); k1r; return ret; }
int drivers_stop_all() { k1s; sceUsbDeactivate(0xCCCC); sceUsbStop(PS3HubDriver.name,0,0); sceUsbStop("USBBusDriver",0,0); sceUsbbdUnregister(&PS3HubDriver); k1r; return 0; }

void stepperfunction(struct passingdata *ptr) { 
	int i;
	
	/* Copy pointer */
	pdata = ptr;	

	/* Switch port */
	if (switch_to_port_delayed >= 0) { /* switch_to_port (dev, dev->switch_to_port_delayed); */ }
	switch_to_port_delayed = -1;
	
	switch (estado) {
		case APP_INITING:
			resetdata(pdata);
			DEBUG("USB Driver 0.1a - (c) DeViaNTe\n");
			
			/* Config our device */
			current_port = 0;
			for (i = 0; i < 6; i++) { localports[i].status = localports[i].change = 0; }
			for (i = 0; i < 7; i++) { portaddress[i] = 0; }
			switch_to_port_delayed = -1;
			comstatus = 0;
			
			/* estado */
			pdata->status = APP_INIT;
			pdata->timeout = 0;
			estado = APP_INIT;
			break;
		case APP_INIT:
			pdata->status = APP_READY;
			pdata->timeout = 0;
			estado = APP_READY;
			break;
		case APP_READY:
			if ( comstatus == 1 ) {
				pdata->timeout = 1000;
				pdata->status = APP_HUB_READY;
				estado = APP_HUB_READY;
			}
			break;
		case APP_HUB_READY:
			if ( pdata->timeout == 0 ) {
				hub_connect_port(1);
				comstatus = 2;
				pdata->status = APP_DEV1_WAIT;
				pdata->timeout = 0;
				estado = APP_DEV1_WAIT;
			}
			else { pdata->status = APP_HUB_READY; pdata->timeout = 0; }
			break;
		case APP_DEV1_WAIT:
			
			break;
	}

	// PASS MANY VARS TO GUI...
	pdata->connected = driver_connected;
	for (i=0;i<6;i++) {
		pdata->ports[i].status = localports[i].status;
		pdata->ports[i].change = localports[i].change;
	}
	
}

/* Init */
int module_start(SceSize args, void *argp) { 
	return 0;
}

int module_stop() { 
	return 0;
}

